<template>
  <!-- 搜索区域 -->
  <ContentWrap>
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="模板名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入模板名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="模板编码" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入模板编码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="邮箱账号" prop="accountId">
        <el-select
          v-model="queryParams.accountId"
          placeholder="请选择邮箱账号"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="account in accountList"
            :key="account.id"
            :value="account.id"
            :label="account.mail"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="开启状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择开启状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in common_enable_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" />
          重置
        </el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['system:mail-template:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" />
          新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['system:mail-template:export']"
        >
          <Icon icon="ep:download" class="mr-5px" />
          导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表区域 -->
  <ContentWrap>
    <el-table v-loading="listLoading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="编号" prop="id" width="80" />
      <el-table-column label="模板编码" prop="code" />
      <el-table-column label="模板名称" prop="name" />
      <el-table-column label="模板标题" prop="title" />
      <el-table-column label="模板内容" prop="content" width="90" :show-overflow-tooltip="false">
        <template #default="scope">
          <el-tooltip
            :content="scope.row.content"
            raw-content
            effect="light"
            trigger="click"
            placement="right-end"
            popper-class="mail-content-popper editor-content-view"
          >
            <el-button link type="info">内容</el-button>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="模板类型" prop="type" width="140" :show-overflow-tooltip="false">
        <template #default="scope">
          <dict-tag :options="system_mail_template_type" :value="scope.row.type" />
        </template>
      </el-table-column>
      <el-table-column label="邮箱账号" prop="accountId">
        <template #default="scope">
          {{ accountList.find((account) => account.id === scope.row.accountId)?.mail }}
        </template>
      </el-table-column>
      <el-table-column label="发送人名称" prop="nickname" width="120" />
      <el-table-column label="开启状态" prop="status" width="90">
        <template #default="scope">
          <dict-tag :options="common_enable_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" prop="createTime" width="180px" />
      <el-table-column label="操作" width="160px" fixed="right">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['system:mail-template:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="primary"
            @click="openSendForm(scope.row.id)"
            v-hasPermi="['system:mail-template:send-mail']"
          >
            测试
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['system:mail-template:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页区域 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <MailTemplateForm ref="editFormRef" @success="getList" />
  <!-- 表单弹窗：测试发送 -->
  <MailTemplateSendForm ref="sendFormRef" />
</template>

<script setup lang="ts">
import { MailTemplateApi, MailTemplateVO } from '@/api/system/mail/mail_template'
import { MailAccountApi, MailAccountVO } from '@/api/system/mail/mail_account'
import MailTemplateForm from './MailTemplateForm.vue'
import MailTemplateSendForm from './MailTemplateSendForm.vue'
import download from '@/utils/download'

/** 邮件模版 列表 */
defineOptions({ name: 'SystemMailTemplate' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const { system_mail_template_type } = useDict(DICT_TYPE.system_mail_template_type)
const { common_enable_status } = useIntDict(DICT_TYPE.common_enable_status)

const listLoading = ref(true) // 列表的加载中状态
const list = ref<MailTemplateVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  name: undefined,
  code: undefined,
  accountId: undefined,
  status: undefined,
  createTime: [],
  pageNo: 1,
  pageSize: 10
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const accountList = ref<MailAccountVO[]>([]) // 邮箱账号列表

/** 查询列表 */
const getList = async () => {
  listLoading.value = true
  try {
    const data = await MailTemplateApi.getMailTemplatePage(queryParams)
    list.value = data.content
    total.value = data.totalElement
  } finally {
    listLoading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const editFormRef = ref<InstanceType<typeof MailTemplateForm>>()
const openForm = (type: string, id?: number) => {
  editFormRef.value?.open(type, id)
}

/** 测试发送操作 */
const sendFormRef = ref<InstanceType<typeof MailTemplateSendForm>>()
const openSendForm = (id: number) => {
  sendFormRef.value?.open(id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 弹出确认框
    await message.delConfirm()
    // 发起删除请求
    await MailTemplateApi.deleteMailTemplate(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await MailTemplateApi.exportMailTemplate(queryParams)
    download.excel(data, '邮件模版数据.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(async () => {
  await getList()
  accountList.value = await MailAccountApi.getMailAccountSimpleList()
})
</script>

<style lang="scss">
.mail-content-popper {
  width: 800px !important;
  height: 400px !important;
  overflow: auto !important;
}
</style>
